var app = getApp()
var util = require('../util.js')

Page({

  /**
   * 组件的初始数据
   */
  data: {
    caption: '2004281155'
  },
  onLoad: function () {
    wx.showModal({
      title: '提示',
      content: '请注意查询格式，目前支持2种查询格式: \r\n1、外箱码格式: 外箱码为10位数字，位于外箱标签上面，可用扫一扫功能扫描获取，如: 1911152505。\r\n2、喷码格式: 见产品上喷码数据，由批号加流水号组成，如: JY012-0061，流水号不够4位加0补充。',
      showCancel: false
    })
  },
  chooseScan(e) {
    var that = this;
    wx.scanCode({
      success(res) {
        that.setData({
          caption: res.result
        })
      }
    })
  },
  formSubmit: function (e) {
    let {
      caption
    } = e.detail.value;
    if (!caption) {
      wx.showToast({
        icon: 'none',
        title: '请输入相关信息！',
      });
      return;
    };
    if (caption.length != 10) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的查询内容！',
      });
      return;
    }
    
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000; 
    var m = timestamp+15;    
     if (m- wx.getStorageSync('m')>0) {
      wx.setStorageSync('m', m+15)
      var that = this;
      wx.cloud.callFunction({
        name: 'GetPost',
        data: {
          data: "fun=1&QueryStr=" + caption
        },
        success: res => {
          console.log(res);
          let obj = JSON.parse(res.result)
          that.setData({ list: obj })
          console.log(that.data.list);
          wx.showToast({
            icon: 'none',
            title: '查询成功！',
          })
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '调用失败',
          })
        }
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请稍等15秒后再查询！',
      });
    }
  }
})
